/***********************************************************************************
 @file   
 @author
 @attention 0-3显示;0
 ***********************************************************************************/
#include "mid_adc.h"
/***********************************************************************************
 * @global param
***********************************************************************************/
_adc_obj adc_obj;

#define P_Q 0.05       //过程噪音 
#define M_R 0.8        //测量噪声    
/***********************************************************************************
* @brief  kalma  p初值不能为0
***********************************************************************************/
float kalman_filter(float data)
{
  static float prevData=0;
  static float p=0.01, q=P_Q, r=M_R, kGain=0; 
    p = p+q;
    kGain = p/(p+r);
 
    data = prevData+(kGain*(data-prevData));
    p = (1-kGain)*p;
 
    prevData = data;
 
    return data;
}
/***********************************************************************************
 * @brief  ADC转换完成中断
***********************************************************************************/
void Adc_IRQHandler(void)
{    
    if(TRUE == Adc_GetIrqStatus(AdcMskIrqJqr))
    {
        Adc_ClrIrqStatus(AdcMskIrqJqr);
        if(sensor_en_read)
        {
            adc_obj.bat_vol       = Adc_GetJqrResult(AdcJQRCH0MUX);
            adc_obj.sensor_vol    = Adc_GetJqrResult(AdcJQRCH1MUX);
            adc_obj.opa_vol_res   = Adc_GetJqrResult(AdcJQRCH2MUX);
            adc_obj.ref_vol       = Adc_GetJqrResult(AdcJQRCH3MUX);
            adc_obj.chge_flag     = 1;               
        }

        Adc_JQR_Start();   
             
    }
}
/***********************************************************************************
 * @brief  ADC模块初始化
***********************************************************************************/
void adc_init(void)
{
     //-----------------------------------------------开启ADC/BGR GPIO外设时钟
    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
    Gpio_SetAnalogMode(GpioPortB, GpioPin12);     //sensor  
    Gpio_SetAnalogMode(GpioPortB, GpioPin13);     //opa
    Gpio_SetAnalogMode(GpioPortB, GpioPin0);      //bat  
    
     //-----------------------------------------------ADC 模块初始化   
    stc_adc_cfg_t              stcAdcCfg;
    DDL_ZERO_STRUCT(stcAdcCfg);
    Sysctrl_SetPeripheralGate(SysctrlPeripheralAdcBgr, TRUE); 
    Bgr_BgrEnable();        ///< 开启BGR
    // ADC 初始化配置
    stcAdcCfg.enAdcMode         = AdcScanMode;                ///<采样模式-扫描
    #if(defined(HARD_WARE)&&(HARD_WARE==HARD_WARE_FIRST))
    stcAdcCfg.enAdcClkDiv       = AdcMskClkDiv2;              
    stcAdcCfg.enAdcSampCycleSel = AdcMskSampCycle4Clk;       
    #else
    stcAdcCfg.enAdcClkDiv       = AdcMskClkDiv2;             
    stcAdcCfg.enAdcSampCycleSel = AdcMskSampCycle4Clk;      
    #endif 
    stcAdcCfg.enAdcRefVolSel    = AdcMskRefVolSelInBgr2p5;     ///<参考电压选择
    stcAdcCfg.enAdcOpBuf        = AdcMskBufDisable;//AdcMskBufEnable           ///<OP BUF配置
    stcAdcCfg.enInRef           = AdcMskInRefEnable;         ///<内部参考电压使能
    stcAdcCfg.enAdcAlign        = AdcAlignRight;              ///<转换结果对齐方式-右
    Adc_Init(&stcAdcCfg);
    
    //-----------------------------------------------ADC 顺序扫描 功能配置
    stc_adc_jqr_cfg_t          stcAdcJqrCfg;
    DDL_ZERO_STRUCT(stcAdcJqrCfg);

    //插队扫描模式功能及通道配置
    //注意：扫描模式下，当配置转换次数为n时，转换通道的配置范围必须为[JQRCH(0)MUX,JQRCH(n-1)MUX]
    stcAdcJqrCfg.bJqrDmaTrig = FALSE;
    stcAdcJqrCfg.u8JqrCnt    = 4;
    Adc_JqrModeCfg(&stcAdcJqrCfg);
    Adc_CfgJqrChannel(AdcJQRCH0MUX, AdcExInputCH8); //bat
    Adc_CfgJqrChannel(AdcJQRCH1MUX, AdcExInputCH19);//sensor
    Adc_CfgJqrChannel(AdcJQRCH2MUX, AdcExInputCH20);//opa
    Adc_CfgJqrChannel(AdcJQRCH3MUX, AdcVref1_2Input);//ref
    ///< ADC 中断使能
    Adc_EnableIrq();
    EnableNvic(ADC_IRQn, IrqLevel3, TRUE);
    
    // 启动插队扫描采样
    Adc_JQR_Start();
}
/***********************************************************************************
 * @brief  ADC取消初始化
***********************************************************************************/
void adc_deinit(void)
{
    delay10us(2);
    M0P_ADC->CR0 = 0;
    M0P_ADC->CR1_f.MODE  = 0;
    M0P_ADC->CR1_f.ALIGN = 0;
}



